/*
 * @Author: Lu junwen
 * @Date: 2022-03-13 18:14:37
 * @LastEditors: Lu junwen
 * @LastEditTime: 2022-04-11 18:26:22
 * @FilePath: \vue-shop-app-customer\src\store\order\order.js
 */
export default {
  namespaced: true,
  state: {
    orderGoods: [],
    arr: false
  },
  mutations: {
    ADD_SHOP_CART(state, payload) {
      // console.log(payload);
      if (state.orderGoods.length == 0) {
        state.orderGoods.push(payload);
      } else {
        state.arr = state.orderGoods.some(x => {
          return x.goods_id == payload.goods_id
        })
        if (state.arr) {
          state.orderGoods.forEach((item, index) => {
            if (payload.goods_id == item.goods_id) {
              state.orderGoods[index].goods_number++
            }
          });
        } else {
          state.orderGoods.push(payload);
        }
      }
    },
    SUB_SHOP_CART(state, payload) {
      state.orderGoods.forEach((item, index) => {
        if (payload == item.goods_id) {
          if (item.goods_number == 1) {
            state.orderGoods.splice(index, 1)
          } else {
            state.orderGoods[index].goods_number--
          }
        }
      })
    },
    RESET_SHOP_CART(state, params) {
      state.orderGoods = []
    }
  },
  actions: {
    addShopcart(context, params) {
      context.commit('ADD_SHOP_CART', params)
    },
    subShopcar(context, params) {
      context.commit('SUB_SHOP_CART', params)
    },
    resetShopCart(context, params) {
      context.commit('RESET_SHOP_CART')
    }
  }
}